#!/bin/bash

# 操作
action="$1"
# 版本
version="$2"


serviceName="gmf-risk-platform-server-lw"
image="$serviceName:$version"

maven_build(){
  rm -rf devops/*.jar
  echo "【开始打包】============================================="
  mvn clean package -Dmaven.test.skip=true
  if [ $? -ne 0 ]; then
    echo "【maven build 失败！】============================================="
    exit
  fi
  cp risk-admin/target/risk-admin.jar devops/
}

# 打镜像
# build_image
docker_build(){
  CID=$(docker ps -a | grep "$serviceName" | awk '{print $1}')
  echo "容器CID $CID"
  if [ -n "$CID" ]
  then
      echo "【容器存在 $serviceName:$CID】================================"
      #停止
      echo "【停止容器 $serviceName:$CID】================================"
      docker stop $CID
      #删除容器
      echo "【删除容器 $serviceName:$CID】================================"
      docker rm $CID
  else
      echo "【容器不存在: $serviceName】==================================="
  fi

  IID=$(docker images | grep "$serviceName" | awk '{print $3}')
  if [ -n "$IID" ]
  then
      echo "【镜像存在 $serviceName:$version:$IID】========================"
      docker rmi $IID
  else
      echo "【镜像不存在 $serviceName:$version】==========================="
  fi
  echo "【build_image $image】==================================="
  docker build -t $image devops/.
}

# 部署
# deploy
deploy(){
    echo "【开始部署】 ======================================================"
    version="$version" docker-compose -f devops/docker-compose.yml up -d
    docker ps |grep $serviceName
}





main(){
    if [ "$action" = "maven_build" ];then
       maven_build
    fi
    if [ "$action" = "docker_build" ];then
       docker_build
    fi

    if [ "$action" = "deploy" ];then
       deploy
    fi
}

main